﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using WMM.Tapioca.Linkbase;
using System.Text.RegularExpressions;
using WMM.Tapioca.Instance;
using WMM.Tapioca.Taxonomy;
using WMM.Tapioca.Utilities;

namespace WMM.Tapioca.Formula.Filters
{
    /// <summary>
    /// The single-measure unit filter can be used to match facts that are reported
    /// with a unit that is specified by a single measure. 
    /// </summary>
    public class SingleMeasureFilter : Filter
    {
        private string measure;

        /// <returns> The measure value. </returns>
        public string Measure
        {
            get { return measure; }
            set { measure = value; }
        }

        private string expression;

        public string Expression
        {
            get { return expression; }
            set { expression = value; }
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        public SingleMeasureFilter(Resource resource)
            : base(resource)
        {
            XElement measureX = resource.ResourceXElement.Descendants().Where(d => d.Parent.Name.LocalName.Equals("measure") && 
            d.Name.LocalName.Equals("qname")).FirstOrDefault();

            if (measureX != null)
            {
                string qname = measureX.Value;
                qname = qname.TrimEnd().TrimStart();
                qname = Regex.Replace(qname, @"\s+", " ");
                measure = qname;
            }

            XElement qnameExpressionX = resource.ResourceXElement.Descendants().Where(d => d.Parent.Name.LocalName.Equals("measure") &&
            d.Name.LocalName.Equals("qnameExpression")).FirstOrDefault();

            if (qnameExpressionX != null)
            {
                string expression = qnameExpressionX.Value;
                expression = expression.TrimEnd().TrimStart();
                expression = Regex.Replace(expression, @"\s+", " ");
                this.expression = expression;
            }
        }


        public override List<FactItem> EvaluateFilter(VariableSet variableSet, List<FactItem> itemsToFilter, DTS dts)
        {
            this.dts = dts;

            List<FactItem> itemListTemp = new List<FactItem>();

            foreach (FactItem item in itemsToFilter)
            {
                if (!item.Concept.SchemaElement.ElementSchemaType.IsNumeric())
                {
                    continue;
                }

                if (this.expression != null)
                {
                    this.measure = EvaluateXPathExpression(variableSet, this.expression, item);
                }

                string measureFilter = Measure.Substring(Measure.IndexOf(':') + 1);

                if (item.UnitRef.Measures.Count == 1)
                {
                    if (Complement ? !item.UnitRef.Measures[0].LocalName.Equals(measureFilter) : item.UnitRef.Measures[0].LocalName.Equals(measureFilter))
                    {
                        itemListTemp.Add(item);
                        continue;
                    }
                }
            }

            return itemListTemp;
        }
    }
}
